package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.anno.NeedLogin;
import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.query.RechargeOfflineQuery;
import cn.wolfcode.p2p.business.service.IPlatformBankInfoService;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import cn.wolfcode.p2p.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RechargeController {
    @Autowired
    private IPlatformBankInfoService platformBankInfoService;
    @Autowired
    private IRechargeOfflineService rechargeOfflineService;

    @RequestMapping("recharge")
    public String recharge(Model model){
        model.addAttribute("banks", platformBankInfoService.listPlatformBankInfo());
        return "recharge/recharge";
    }

    @RequestMapping("recharge_save")
    @ResponseBody
    @NeedLogin
    public JsonResult rechargeSave(RechargeOffline ro){
        JsonResult jsonResult = new JsonResult();
        try {
            rechargeOfflineService.save(ro);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }

    @RequestMapping("recharge_list")
    public String rechargeList(Model model, @ModelAttribute("qo") RechargeOfflineQuery qo){
        model.addAttribute("pageInfo", rechargeOfflineService.query(qo));
        return "recharge/recharge_list";
    }
}
